package com.fs.web.base.service;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.baseMapper.UserMapper;
import com.fs.common.pojo.Product;
import com.fs.common.pojo.User;
import com.fs.common.yzMapper.ProductMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 清理数据业务类
 */
@Service
public class CleanService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;

    public void clean() {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("type","basic");
        Product product = productMapper.selectOne(wrapper);
        List<User> users = userMapper.selectList(null);
        String yesterday = DateUtil.yesterday().toString();
        if(CollectionUtils.isNotEmpty(users)){
            for (User user : users) {
                if(StringUtils.isNoneBlank(user.getEndTime())){
                    long betweenDay = DateUtil.between(DateUtil.parse(user.getEndTime()), DateUtil.parse(yesterday), DateUnit.DAY);
                    if(betweenDay > 90){
                        user.setStartTime("");
                        user.setEndTime("");
                        userMapper.updateById(user);
                    }else if(betweenDay > 60){
                        user.setCount(0);
                        user.setProductId(product.getId());
                        userMapper.updateById(user);
                    }
                }
            }
        }
    }
}
